using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Text;
using System.Windows.Forms;
using Jonathan.Classes;
using System.Drawing;
using Jonathan.Properties;

namespace Jonathan.Controls
{
   /// <summary>
   /// A Control that will Display the users book marks.
   /// </summary>
   public class CtrlBookMark : Control
   {
      private OutlookGrid.OutlookGrid m_GridBookMarks;
      private readonly FrmMain m_FormMain;
      private readonly BookMarkDropDown m_DropDown;

      /// <summary>
      /// Creates a new Book Mark Control.
      /// </summary>
      /// <param name="main">Reference to Main Form.</param>
      /// <param name="dropDown">DropDown Control is attached to.</param>
      public CtrlBookMark (FrmMain main, BookMarkDropDown dropDown)
      {
         this.m_FormMain = main;
         this.m_DropDown = dropDown;
         this.m_GridBookMarks = new OutlookGrid.OutlookGrid { Dock = DockStyle.Fill };
         this.SetupGrid ();
         this.Width = 300;
         this.Height = 300;
         this.m_GridBookMarks.CellClick += GridBookMarksCellClick;

         this.Controls.Add (m_GridBookMarks);
      }

      /// <summary>
      /// Sets up meta data for the Grid...
      /// </summary>
      private void SetupGrid ()
      {
         this.m_GridBookMarks.EmptyGridText = "No Book Marks Created.";
         this.m_GridBookMarks.AllowUserToAddRows = false;
         this.m_GridBookMarks.AllowUserToDeleteRows = false;
         this.m_GridBookMarks.AllowUserToResizeColumns = false;
         this.m_GridBookMarks.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
         this.m_GridBookMarks.BackgroundColor = Color.White;
         this.m_GridBookMarks.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
         this.m_GridBookMarks.CollapseIcon = null;
         this.m_GridBookMarks.ColumnHeadersVisible = false;
         this.m_GridBookMarks.BorderStyle = BorderStyle.None;
         DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle
            {
               Alignment = DataGridViewContentAlignment.TopLeft,
               BackColor = SystemColors.Window,
               Font = new Font ("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0),
               ForeColor = SystemColors.ControlText,
               SelectionBackColor = SystemColors.Highlight,
               SelectionForeColor = SystemColors.HighlightText,
               WrapMode = DataGridViewTriState.True
            };
         this.m_GridBookMarks.DefaultCellStyle = dataGridViewCellStyle1;
         this.m_GridBookMarks.Dock = DockStyle.Fill;
         this.m_GridBookMarks.EditMode = DataGridViewEditMode.EditProgrammatically;
         this.m_GridBookMarks.ExpandIcon = null;
         this.m_GridBookMarks.GridColor = Color.LightGray;
         this.m_GridBookMarks.Location = new Point (0, 0);
         this.m_GridBookMarks.Name = "m_SearchGrid";
         this.m_GridBookMarks.RowHeadersVisible = false;
         this.m_GridBookMarks.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
         this.m_GridBookMarks.Size = new Size (1861, 1494);
         this.m_GridBookMarks.TabIndex = 6;
         this.m_GridBookMarks.ScrollBars = ScrollBars.None;
         this.m_GridBookMarks.CanCollapse = false;
         //this._gridBookMarks.ExpandIcon = this.m_FormMain.GetInternalImage("plus");
         //this._gridBookMarks.CollapseIcon = this.m_FormMain.GetInternalImage("minus");

         this.AddColumns ();
      }

      /// <summary>
      /// Adds the Columns for the Grid...
      /// </summary>
      private void AddColumns ()
      {
         this.m_GridBookMarks.Columns.Add ("colVersion", "Version");
         this.m_GridBookMarks.Columns.Add ("colInfo", "Passage");
         // Quick Lookup
         DataGridViewImageColumn colILookUp = new DataGridViewImageColumn
            {
               Name = "colLookUp",
               HeaderText = "Look Up",
               Width = 22
            };
         this.m_GridBookMarks.Columns.Add (colILookUp);
         // Goto
         DataGridViewImageColumn colIGoto = new DataGridViewImageColumn
            {
               Name = "colGoto",
               HeaderText = Resources.Goto,
               Width = 16
            };
         this.m_GridBookMarks.Columns.Add (colIGoto);
         // Remove
         DataGridViewImageColumn colIRemove = new DataGridViewImageColumn
            {
               Name = "colRemove",
               HeaderText = "Remove",
               Width = 16
            };
         this.m_GridBookMarks.Columns.Add (colIRemove);

         this.m_GridBookMarks.GroupTemplate.Column = this.m_GridBookMarks.Columns[0];
         this.m_GridBookMarks.Columns[0].Visible = false;
         this.m_GridBookMarks.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
      }

      /// <summary>
      /// Occurs when a cell in the bookmarks grid is clicked.
      /// </summary>
      /// <param name="sender">Cell Clicked</param>
      /// <param name="e">DataGridViewCellEventArgs</param>
      void GridBookMarksCellClick (object sender, DataGridViewCellEventArgs e)
      {
         if (e.ColumnIndex == 2)
         {
            this.m_DropDown.CloseDropDown ();
            List<CPassage> passages = new List<CPassage> {(CPassage) this.m_GridBookMarks.Rows [e.RowIndex].Tag};
            this.m_FormMain.StartBible (passages, null);
         }
         else if (e.ColumnIndex == 3)
         {
            CPassage passage = (CPassage)this.m_GridBookMarks.Rows[e.RowIndex].Tag;
            string dbPath = this.m_FormMain.GetDbPath (passage.Version);
            if (!string.IsNullOrEmpty (dbPath))
            {
               List<CPassage> passages = new List<CPassage> {passage};
               using (SQLiteConnection dbConn = new SQLiteConnection (Common.ConnString (dbPath)))
               {
                  dbConn.Open ();
                  StringBuilder sb = Common.GetToolTipText (passages, passage.Version, dbConn.CreateCommand ());

                  fHtmlToolTip tt = new fHtmlToolTip (this.m_FormMain, this.m_GridBookMarks.PointToScreen (
                     new Point (this.m_GridBookMarks.Right + 3,
                                this.m_GridBookMarks.Rows [e.RowIndex].Height *
                                this.m_GridBookMarks.Rows [e.RowIndex].Index)),
                                                      sb.ToString (), passage.ToString (), true);
                  tt.ShowTT ();
               }
            }
         }
         else if (e.ColumnIndex == 4)
         {
            this.m_DropDown.CloseDropDown ();
            this.m_FormMain.RemoveBookMark ((CPassage)this.m_GridBookMarks.Rows[e.RowIndex].Tag);
         }

         this.m_GridBookMarks.ClearSelection ();
      }

      /// <summary>
      /// Occurs when the background color changes, set the grid to the controls background color.
      /// </summary>
      /// <param name="e">Event Arguments</param>
      protected override void OnBackColorChanged (EventArgs e)
      {
         base.OnBackColorChanged (e);
         this.m_GridBookMarks.BackgroundColor = this.BackColor;
      }

      /// <summary>
      /// Adds the given Book Mark...
      /// </summary>
      /// <param name="bm">Book Mark to Add.</param>
      public void AddBookMark (CPassage bm)
      {
         OutlookGrid.OutlookGridRow row = new OutlookGrid.OutlookGridRow ();
         row.CreateCells (this.m_GridBookMarks, new object[] { bm.Version, bm.Book + " " + bm.Chapter + ":" + bm.Verse, 
                InternalImages.gotoBookMark, InternalImages.chapter, InternalImages.DeleteHS });
         row.Tag = bm;
         row.Cells[2].ToolTipText = "Goto Book Mark";
         row.Cells[3].ToolTipText = "Look up Book Mark";
         row.Cells[4].ToolTipText = "Delete Book Mark";
         this.m_GridBookMarks.Rows.Add (row);
      }

      /// <summary>
      /// Sorts the Grid...
      /// </summary>
      public void SortGrid ()
      {
         if (this.m_GridBookMarks.Rows.Count > 0)
         {
            //this._gridBookMarks.Sort(new PassageGridComparer(ListSortDirection.Ascending,
            //    this.m_FormMain.GetDBPath(this._gridBookMarks.Rows[0].Cells[0].Value.ToString())));

            int rowHeight = this.m_GridBookMarks.Rows[0].Height;
            if ((rowHeight * this.m_GridBookMarks.Rows.Count) > 300)
            {
               this.m_GridBookMarks.ScrollBars = ScrollBars.Vertical;
               this.m_GridBookMarks.Height = 300;
               this.Height = 300;
            }
            else
            {
               this.m_GridBookMarks.ScrollBars = ScrollBars.None;
               this.m_GridBookMarks.Height = (this.m_GridBookMarks.Rows.Count + 1) * rowHeight;
               this.Height = this.m_GridBookMarks.Rows.Count * rowHeight;
            }
         }
      }

      /// <summary>
      /// Clears the information in the grid (re-adds the columns)...
      /// </summary>
      public void ClearGrid ()
      {
         this.m_GridBookMarks.BindData (null, null);
         this.m_GridBookMarks.ClearGroups ();
         this.m_GridBookMarks.Rows.Clear ();
         this.AddColumns ();
      }

      /// <summary>
      /// Gets or Sets the OutlookGrid...
      /// </summary>
      public OutlookGrid.OutlookGrid Grid
      {
         get { return this.m_GridBookMarks; }
         set { this.m_GridBookMarks = value; }
      }
   }
}
